# -*- coding: utf-8 -*-

import datetime,time,functools

from django.db import models
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.contrib import admin
from django.template import RequestContext, Context, loader,Template


""" Signal handling """

import cms.models

def my_test_handler(sender, **kwargs):
    print "Signal > PreRenderSignal at view", kwargs['view'].__name__
    o=kwargs['context']['obj']
    t=Test(name=o.name)
    t.save()
    o.name='altered by signal subscriber :-)'

cms.models.PreRenderSignal.connect(my_test_handler)


import django.db.models.signals

def handle_build_in(signaltype):
    red,white,green=('\033[31m','\033[37m','\033[32m')
    def handle_build_in_closure(sender, **kwargs):
        pass
        #print "Signal >",red,signaltype,white,
        #print
    return(handle_build_in_closure)

django.db.models.signals.pre_init.connect( handle_build_in('pre_init') , weak=False )
django.db.models.signals.post_init.connect( handle_build_in('post_init') , weak=False )

django.db.models.signals.pre_save.connect( handle_build_in('pre_save') , weak=False )
django.db.models.signals.post_save.connect( handle_build_in('post_save') , weak=False )


""" Models """

class Test(models.Model):
    cdate = models.DateTimeField('Dato oprettet',auto_now_add=True)
    name = models.CharField('Navn',max_length=50)

    class Meta:
        get_latest_by='cdate'
        ordering = ('name',)

    def __unicode__(self):
        return self.name
